﻿using SD.Toolkits.Sql.SqlServer;
using System.IO;

namespace SlamDunk.MES.DataDrive.Common
{
    /// <summary>
    /// 数据库操作帮助类
    /// </summary>
    public static class DataBaseHelper
    {
        #region # 备份数据库 —— void BackupsDataBase(string connectionString, string dataBaseName, string backFilePath)

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="dataBaseName">数据库名称</param>
        /// <param name="backFilePath">备份文件路径</param>
        public static void BackupsDataBase(string connectionString, string dataBaseName, string backFilePath)
        {
            string sql = @"Backup Database [" + dataBaseName + @"]　To disk='" + backFilePath + "'";
            SqlServerHelper sqlHelper = new SqlServerHelper(connectionString);
            sqlHelper.ExecuteNonQuery(sql);
        }

        #endregion

        #region # 备份数据库 —— void BackupsDataBase(DataMenu dataMenu, string backFilePath)

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="dataMenu">数据菜单</param>
        /// <param name="backFilePath">备份文件路径</param>
        public static void BackupsDataBase(DataMenu dataMenu, string backFilePath)
        {
            if (File.Exists(backFilePath))
            {
                File.Delete(backFilePath);
            }
            string sql = @"Backup Database [" + dataMenu.TypeDataTag.DataBaseName + @"]　To disk='" + backFilePath + "'\r";
            if (dataMenu.MethodDataTag.AttachedDataBaseParam != null)
            {
                foreach (var attachedDataBaseName in dataMenu.MethodDataTag.AttachedDataBaseParam)
                {
                    string attachedBackFilePath = $"{dataMenu.MethodDataTag.Sort}-{dataMenu.TypeDataTag.DataBaseName}-{dataMenu.MethodDataTag.Name}[{attachedDataBaseName}].bak"; ;
                    sql += @"Backup Database [" + attachedDataBaseName + @"]　To disk='" + attachedBackFilePath + "'\r";

                    if (File.Exists(attachedBackFilePath))
                    {
                        File.Delete(attachedBackFilePath);
                    }
                }
            }
            SqlServerHelper sqlHelper = new SqlServerHelper(dataMenu.TypeDataTag.ConnectionStrings);
            sqlHelper.ExecuteNonQuery(sql);
        }

        #endregion

        #region # 清除工作流数据库 —— void ClearWorkflowDataBase(DataMenu dataMenu, string backFilePath)

        /// <summary>
        /// 清除工作流数据库
        /// </summary>
        public static void ClearWorkflowDataBase(string connectionStrings)
        {
            string sql =
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[IdentityOwnerTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[InstanceMetadataChangesTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[InstancePromotedPropertiesTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[InstancesTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[KeysTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[LockOwnersTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[RunnableInstancesTable]\r" +
            "TRUNCATE TABLE[System.Activities.DurableInstancing].[ServiceDeploymentsTable]\r";
            SqlServerHelper sqlHelper = new SqlServerHelper(connectionStrings);
            sqlHelper.ExecuteNonQuery(sql);
        }

        #endregion
    }
}
